﻿using System.Collections.Generic;
using System.Linq;
using Model.Entities;
using Model.Repositories;

namespace Presenter.Users
{
    public class UserDiaryMessagesPresenter
    {
        private IUserDiaryMessagesView _view;

        public UserDiaryMessagesPresenter(IUserDiaryMessagesView view, int id)
        {
            _view = view;

            _view.GetMessages(GetMessages(id));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        public UserDiaryMessagesPresenter(IUserDiaryMessagesView view, string email, string password)
        {
            _view = view;

            _view.GetMessages(GetMessages(email, password));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        void ViewEntityCreated(string email, string password, string title, string text)
        {
            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                obj.DiaryMessages.Add(new DiaryMessage
                                          {
                                              Sender = obj,
                                              Text   = text,
                                              Title  = title 
                                          });
                RepositoryManager.Instance.UserRepository.Update(obj);
            }
        }

        void ViewEntityDeleted(string email, string password, int id)
        {
            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                obj.DiaryMessages.Remove(obj.DiaryMessages.FirstOrDefault(o => o.Id == id));
                RepositoryManager.Instance.UserRepository.Update(obj);
            }
        }

        public void Reload(IUserDiaryMessagesView view, int id)
        {
            _view = view;

            _view.GetMessages(GetMessages(id));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        public void Reload(IUserDiaryMessagesView view, string email, string password)
        {
            _view = view;

            _view.GetMessages(GetMessages(email, password));
            _view.EntityDeleted += ViewEntityDeleted;
            _view.EntityCreated += ViewEntityCreated;
        }

        List<List<string>> GetMessages(int id)
        {
            var result = new List<List<string>>();

            var obj = RepositoryManager.Instance.UserRepository.Get(id);
            if (obj != null)
            {
                result.AddRange(obj.DiaryMessages.Select(message => new List<string> {message.Id.ToString(), message.Title, message.Text, message.SentDateTime.ToString()}));
            }

            return result;
        }

        List<List<string>> GetMessages(string email, string password)
        {
            var result = new List<List<string>>();

            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                result.AddRange(obj.DiaryMessages.Select(message => new List<string> { message.Id.ToString(), message.Title, message.Text, message.SentDateTime.ToString() }));
            }

            return result;
        }
    }
}

